﻿using KYJBLL;
using KYJScadaProModbusRtu;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using thinger.DataConvertLib;

namespace KYJScadaProModbusRTU
{
    public partial class FrmHistoryLog : Form
    {
        public FrmHistoryLog()
        {
            InitializeComponent();

            this.cmb_LogType.Items.Add("全部信息");
            this.cmb_LogType.Items.Add("系统日志");
            this.cmb_LogType.Items.Add("操作日志");
            this.cmb_LogType.Items.Add("系统报警");

            this.dgv_Main.AutoGenerateColumns = false;

            this.cmb_LogType.SelectedIndex = 0;

            this.dtp_Start.Value = DateTime.Now.AddHours(-2.0f);
            this.dtp_End.Value = DateTime.Now;
        }

        private SysLogManage sysLogManage = new SysLogManage();

        private void btn_Query_Click(object sender, EventArgs e)
        {
            string start = this.dtp_Start.Text;
            string end = this.dtp_End.Text;

            string logType = this.cmb_LogType.SelectedIndex == 0 ? "" : this.cmb_LogType.Text;

            Task<OperateResult<DataTable>> task = Task.Run(() =>
            {
                return QueryProcess(start, end, logType);

            });
            var result = task.GetAwaiter().GetResult();
            if (result.IsSuccess)
            {
                this.dgv_Main.DataSource = null;
                this.dgv_Main.DataSource = result.Content;
            }
            else
            {
                new FrmMessageBoxWithoutAck("查询出错：" + result.Message, "错误提示").ShowDialog();
            }
        }

        private OperateResult<DataTable> QueryProcess(string start, string end, string logType)
        {
            //判断时间
            DateTime startTime = Convert.ToDateTime(start);
            DateTime endTime = Convert.ToDateTime(end);

            if (startTime > endTime)
            {
                return OperateResult.CreateFailResult<DataTable>("开始时间不能大于结束时间");
            }

            TimeSpan timeSpan = endTime - startTime;

            if (timeSpan.TotalHours > 24.0)
            {
                return OperateResult.CreateFailResult<DataTable>("查询时间不能超过24小时");
            }

            DataTable dataTable = sysLogManage.QuerySysLogByConditon(start, end, logType);

            if (dataTable != null)
            {
                return OperateResult.CreateSuccessResult<DataTable>(dataTable);
            }
            else
            {
                return OperateResult.CreateFailResult<DataTable>("未能查到数据，请检测");
            }
        }

        private void dgv_Main_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            DataGridViewHelper.DgvRowPostPaint((DataGridView)sender, e);
        }

        private void dgv_Main_Paint(object sender, PaintEventArgs e)
        {
            DataGridViewHelper.DgvRowPaint((DataGridView)sender, e, Color.FromArgb(1, 146, 235));
        }

        private void dgv_Main_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.RowIndex != -1 && e.ColumnIndex >= 5)
            {
                object value = this.dgv_Main.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;

                if (value == null || value?.ToString().Length == 0)
                {
                    e.Value = "--";
                }
            }
        }

        private void btn_ExPort_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Xls文件(*.xls)|*.xls|所有文件|*.*";
            saveFileDialog.Title = "导出日志报表";
            saveFileDialog.FileName = "日志报表" + DateTime.Now.ToString("yyyyMMddHHmmss");
            saveFileDialog.DefaultExt = "xls";
            saveFileDialog.AddExtension = true;
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                if (ToolsLib.ExcelHelper.DataGridViewToExcel(saveFileDialog.FileName, this.dgv_Main))
                {
                    Process.Start(saveFileDialog.FileName);
                }
                else
                {
                    new FrmMessageBoxWithoutAck("日志报表导出失败", "日志报表导出").ShowDialog();
                }
            }
        }

        #region 减少闪烁
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams parss = base.CreateParams;
                parss.ExStyle |= 0x02000000;
                return parss;
            }
        }
        #endregion
    }
}
